$(function() {
	genVal();
	timeControl();

	// beginTimeChangeAction();
	// endTimeChangeAction()
	// beginTimeChange();
	// endTimeChange();
	 timeChange();
});

/**通过defaultValue得到自己的option(class=selectedTime为必须),初始化时用**/
function genVal() {
	
	$(".selectedTime").each(function(index, object) {
		var defaultValue = $(object).attr('defaultValue');
		$(object).children('option').each(function(i, o) {
			if ($(o).attr('value') === defaultValue) {
				$(o).attr('selected', 'selected');
			}
		});
	});
	
	//from 和  to 均有defaultValue了，这时通过对方已选 的生成minTime和maxTime
	$(".selectedTime").each(function(index, object) {
		timeChangeAction(object);
	});
}

/**根据minTime和maxTime得到selectedTime的option的列表(class=selectedTime为必须) **/
function timeControl() {
	$('.selectedTime').each(function(index, element) {
		genMonthList(element);//生成列表 
		
		var minTime = $(element).attr('minTime');
		var maxTime = $(element).attr('maxTime');
		var indexArray = new Array();
		$(element).children().each(function(i, o) {//option
			// $(o).css('display', 'none');			 // $(o).attr('disabled','true');			optionVal = $(o).attr('value');
			if (optionVal.localeCompare(minTime) < 0 || optionVal.localeCompare(maxTime) > 0) {//optionVal >=minTime&&optionVal<=maxTime ,要加入
				indexArray.push(i);
				//$(o).css('display', 'block');
				 $(o).remove();
				// $(o).removeAttr('disabled');
			}
		});
	});
}

function timeChange() {
	$('.selectedTime').each(function(index, object) {//object为一个select
		$(object).change(function() {
			timeChangeAction(object);
		});
	});
}

/*from 和 to 均根据对方的当前日期来 获得minTime和maxTime**/
function timeChangeAction(object){
		range = parseInt($(object).attr('range'));
		var SelectedVal = $(object).children("option:selected").attr('value');
			//选择的val
			year = SelectedVal.substr(0, 4);
			month = SelectedVal.substr(5, 7);

			var toComponentId = $(object).attr('toComponentId');
			var fromComponentId = $(object).attr('fromComponentId');

			var nextTime = new Date(year + '/' + month + '/01');
			
			if (toComponentId) {
				//+6个月后的时间
				nextTime.setMonth(nextTime.getMonth() + range + 1);
				nextyear = nextTime.getFullYear();
				//6月后的年
				nextmonth = nextTime.getMonth();
				//6月的月
				nextmonth = nextmonth >= 10 ? nextmonth : '0' + nextmonth;
				tmpTime = nextyear + '-' + nextmonth;
				$('#' + toComponentId).attr('minTime', SelectedVal);
				//设置最大值约束
				$('#' + toComponentId).attr('maxTime', tmpTime);
				
//			alert(SelectedVal + "," + tmpTime);

			} else if (fromComponentId) {
				//-6个月后的时间
				nextTime.setMonth(nextTime.getMonth() - range+1);
				nextyear = nextTime.getFullYear();
				//6月前的年
				nextmonth = nextTime.getMonth();
				//6月前的月
				nextmonth = nextmonth >= 10 ? nextmonth : '0' + nextmonth;
				tmpTime = nextyear + '-' + nextmonth;
				$('#' + fromComponentId).attr('minTime', tmpTime);
				$('#' + fromComponentId).attr('maxTime', SelectedVal);
//				alert(tmpTime + "," + SelectedVal);
			}
			timeControl();
}


/**生成列表 **/
function genMonthList(object){
	var selectVal = $(object).children('option:selected').attr('value');
	$(object).empty();
	var min = $(object).attr('min');
	var minArray = min.split('-');
	
	var max = $(object).attr('max');
	var maxArray = max.split('-');
	
	var minDate = new Date(minArray[0] + '/' + minArray[1] + '/01');
	var maxDate = new Date(maxArray[0] + '/' + maxArray[1] + '/01');
	
	var htmlStr = "";
	while(minDate.getTime()<=maxDate.getTime()){  //生成列表 
		minYear = minDate.getFullYear();
		minMonth = minDate.getMonth()+1;
		minMonth = minMonth >= 10 ? minMonth : '0' + minMonth;
		minStr = minYear+"-"+minMonth;
		htmlStr+="<option value="+minStr+">";
		htmlStr+=minStr+"</option>";
		
		minDate.setMonth(minDate.getMonth() + 1); //月份加一
	}
	$(object).append(htmlStr);
	//
	$(object).children('option').each(function(i,o){
		if ($(o).attr('value') === selectVal) {
				$(o).attr('selected', 'selected');
		}
	});
}


// 
	// $('#beginTime').change(function() {
		// beginTimeChangeAction(this);
	// });
// 
// /**得到beginTime的当前option,得到对方的上下限**/
// function beginTimeChangeAction() {
	// var beginTimeSelectedVal = $('#beginTime').children("option:selected").attr('value');
	// year = beginTimeSelectedVal.substr(0, 4);
	// month = beginTimeSelectedVal.substr(5, 7);
// 
	// var nextTime = new Date(year + '/' + month + '/01');
	// nextTime.setMonth(nextTime.getMonth() + 8);
	// //+6个月后的时间
// 
	// nextyear = nextTime.getYear() + 1900;
	// //6月后的年
	// nextmonth = nextTime.getMonth();
	// //6月的月
	// nextmonth = nextmonth >= 10 ? nextmonth : '0' + nextmonth;
	// tmpTime = nextyear + '-' + nextmonth;
	// //	alert(tmpTime);
// 
	// $('#endTime').attr('minTime', beginTimeSelectedVal);
	// $('#endTime').attr('maxTime', tmpTime);
	// //设置最大值约束
	// alert(beginTimeSelectedVal + "," + tmpTime);
	// timeControl();
// }
// 
// function endTimeChange() {
	// $('#endTime').change(function() {
		// endTimeChangeAction();
	// });
// }
// 
// /**得到endTime的当前option,得到对方的上下限**/
// function endTimeChangeAction() {
	// var endTimeSelectedVal = $('#endTime').children("option:selected").attr('value');
	// year = endTimeSelectedVal.substr(0, 4);
	// month = endTimeSelectedVal.substr(5, 7);
// 
	// var nextTime = new Date(year + '/' + month + '/01');
	// nextTime.setMonth(nextTime.getMonth() - 6);
	// //-6个月后的时间
// 
	// nextyear = nextTime.getYear() + 1900;
	// //6月前的年
	// nextmonth = nextTime.getMonth();
	// //6月前的月
	// nextmonth = nextmonth >= 10 ? nextmonth : '0' + nextmonth;
	// tmpTime = nextyear + '-' + nextmonth;
	// //	alert(tmpTime);
// 
	// $('#beginTime').attr('maxTime', endTimeSelectedVal);
	// $('#beginTime').attr('minTime', tmpTime);
	// alert(tmpTime + "," + endTimeSelectedVal);
	// timeControl();
// }

